//
//  HouseDetailCell2.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/12.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "HouseDetailCell2.h"
#import <MapKit/MapKit.h>
#import "CustomAnnotation.h"




@interface HouseDetailCell2 ()

@property (weak, nonatomic) IBOutlet MKMapView *mapview;
@property (nonatomic,assign)CGFloat zome;

@end


@implementation HouseDetailCell2

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    HouseDetailCell2 *cell = [[[NSBundle mainBundle]loadNibNamed:@"HouseDetailCell2" owner:nil options:nil]lastObject];
    
    cell.mapview.mapType = MKMapTypeStandard;
    cell.mapview.zoomEnabled=false;
    cell.mapview.showsUserLocation=YES;
    [cell.mapview setUserInteractionEnabled:false];
    
    return cell;
    
}

+(CGFloat)getHight{

    
    return  254;
}

-(void)setData:(HouseTypeData *)data
{
    _data = data;
    
//    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);
     CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(data.latitude,data.longitude);
    float zoomLevel = 0.02f;
    self.zome = zoomLevel;
//    MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
    MKCoordinateRegion adjustedRegion = MKCoordinateRegionMakeWithDistance(coords, 40000, 40000);
    
    adjustedRegion = [self.mapview regionThatFits:adjustedRegion];
    if((adjustedRegion.center.latitude >= -90) && (adjustedRegion.center.latitude <= 90) && (adjustedRegion.center.longitude >= -180) && (adjustedRegion.center.longitude <= 180))
    {
        [self.mapview setRegion:adjustedRegion animated:YES];
    }
    else
    {
        NSLog(@"invilid region");
    }
    
    [self createAnnotationWithCoords:coords];
}

#pragma mark添加大头针
-(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords
{
    CustomAnnotation *annotation = [[CustomAnnotation alloc]initWithCoordinate:coords];
    [self.mapview addAnnotation:annotation];
}
#pragma mark 点击放大地图
- (IBAction)toBigBtnOnClick:(UIButton *)sender {
    
    CGFloat margin = 0.001;
    if (self.zome - margin <= 0) {
        return;
    }
    self.zome -= margin;
    
    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);
    MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(self.zome, self.zome));
    
    region = [self.mapview regionThatFits:region];
    
    [self.mapview setRegion:region animated:TRUE];
    [self createAnnotationWithCoords:coords];
    
}


@end
